<!--
  功能：歌单、歌手详情页
  作者：邹惜财
  gitee：gitee.com/gujun-zxc
  时间：2020年05月24日 16:16:15
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
<div class='music-list'>
 <div class="back" @click="back">
  <i class="iconfont icon-icon_on_the_left"></i>
 </div>
 <h1 class="title" v-html="title"></h1>
 <div class="bg-image" :style="bgStyle" ref="bgImage">
  <div class="filter" ref="filter"></div>
 </div>
 <div class="bg-layer" ref="layer"></div>
  <scroll @scroll="scroll" :probe-type="probeType" :listen-scroll="listenScroll" :data="songs" class="list" ref="list">
    <div class="song-list-wrapper">
          <div class="play">
            <i class="iconfont icon-bofang"></i>
            <span class="text">播放全部</span>
            <span class="count">(共首)</span>
          </div>
      <song-list :songs="songs"></song-list>
    </div>
  </scroll>
</div>
</template>

<script>
import {mapGetters} from 'vuex'
import Scroll from 'base/scroll/scroll'
import SongList from 'base/song-list/song-list'
const RESERVED_HEIGHT = 44
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {
    bgImage: {
      type: String,
      default: ''
    },
    songs: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: ''
    }
  },
  // 局部注册的组件
  components: {
    Scroll,
    SongList
  },
  // 组件状态值
  data () {
    return {
      scrollY: 0
    }
  },
  // 计算属性
  computed: {
    bgStyle() {
      return `background-image:url(${this.bgImage})`
    },
    ...mapGetters([
      'singer'
    ])
  },
  // 侦听器
  watch: {
    scrollY(newY) {
      // layer的标签最大不能超tranlateY
      let tranlateY = Math.max(this.minTranslateY, newY)
      // z-index
      let zIndex = 0
      let scale = 1
      let blur = 0
      // 给标签layer设置动画效果
      this.$refs.layer.style['transform'] = `translate3d(0,${tranlateY}px,0)`
      this.$refs.layer.style['webkiTransform'] = `translate3d(0,${tranlateY}px,0)`

      // 往下滑图片放大效果
      // Math.abs(x)获取x的绝对值
      const percent = Math.abs(newY / this.imageHeight)
      console.log(percent)
      // 往下滑newY是正数
      if (newY > 0) {
        scale = 1 + percent
        zIndex = 10
      } else {
        blur = Math.min(20 * percent, 20)
      }
      this.$refs.filter.style['backdrop-filter'] = `blur(${blur}px)`
      this.$refs.filter.style['webkiBackdrop-filter'] = `blur(${blur}px)`
      // 当newY<minTranslateY时将图片高度设置为RESERVED_HEIGHT
      if (newY < this.minTranslateY) {
        // /原本bgImage的高度是由padding-top="70%"撑起来的，现在改用height。
        zIndex = 10
        this.$refs.bgImage.style.paddingTop = 0
        this.$refs.bgImage.style.height = `${RESERVED_HEIGHT}px`
      } else {
        this.$refs.bgImage.style.paddingTop = '70%'
        this.$refs.bgImage.style.height = 0
      }
      this.$refs.bgImage.style.zIndex = zIndex

      // 往下滑图片放大效果
      // transform——动画+scale——缩放 =动态缩放
      this.$refs.bgImage.style['transform'] = `scale(${scale})`
      this.$refs.bgImage.style['webkiTransform'] = `scale(${scale})`
    }
  },
  // 组件方法
  methods: {
    scroll(pos) {
      // console.log(pos.y)
      this.scrollY = pos.y
    },
    back () {
      this.$router.back()
    }
  },
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.probeType = 3
    this.listenScroll = true
  },
  /**
  * 在挂载开始之前被调用：相关的 render 函数首次被调用。
  */
  beforeMount () {
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
    //  获取图片的高度
    this.imageHeight = this.$refs.bgImage.clientHeight
    // 获取 layer超出歌手组件的高度（此高度为负值）
    this.minTranslateY = -this.imageHeight + RESERVED_HEIGHT
    // 设置图片父标签的高度
    this.$refs.list.$el.style.top = `${this.imageHeight}px`
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable'
@import '~common/stylus/mixin'
.music-list
    position: fixed
    z-index: 100
    top: 0
    left: 0
    bottom: 0
    right: 0
    background: $color-text-lll
    .back
      position absolute
      top: 2px
      left: 6px
      z-index: 50
      .icon-icon_on_the_left
        display: block
        padding: 10px
        font-size: $font-size-large-x
        color: $color-text-l
    .title
      position: absolute
      top: 0
      left: 10%
      z-index: 40
      width: 80%
      no-wrap()
      text-align: center
      line-height: 40px
      font-size: $font-size-large
      color: $color-text
    .bg-image
      position: relative
      width: 100%
      height: 0
      padding-top: 70%
      transform-origin: top
      background-size: cover
      .filter
        position: absolute
        top: 0
        left: 0
        width: 100%
        height: 100%
        background: rgba(7, 17, 27, 0.4)
    .bg-layer
      position: relative
      height: 100%
      background: $color-text-lll
    .list
      position: fixed
      top: 0
      bottom: 0
      width: 100%
      background: $color-text-lll
      .song-list-wrapper
        padding: 41px 0 20px 0;
        border-radius: 10px;
        position: relative;
        top:-20px;
        .play
          position: absolute;
          top: 0px;
          display: flex;
          align-items: center;
          width: 100%;
          height: 40px;
          padding-left: 16px;
          border-bottom: 1px solid rgb(228, 228, 228);
          .iconfont
            font-size: 18px;
            padding-right: 14px;
          .text
            font-size: $font-size-medium-x;
          .count
            font-size: $font-size-medium;
            color: $color-text-g
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
